//
//  LoginViewController.m
//  北洋信息墙
//
//  Created by 陈旭 on 13-10-9.
//  Copyright (c) 2013年 陈旭. All rights reserved.
//

#import "LoginViewController.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
#import "NetAPIManager.h"
#import <POP/POP.h>
@interface LoginViewController ()
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    /*
     自定义该页面
    */
    self.myLogin = [[Login alloc]init];
    
    //UI设置
    NSString* fontName = @"Helvetica";
    
    //[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"login_back"]]];
    

    
    
    self.usernameField.layer.cornerRadius = 3.0f;
    self.usernameField.placeholder = @"用户名";
    self.usernameField.leftViewMode = UITextFieldViewModeAlways;
    UIView* leftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.usernameField.leftView = leftView1;
    self.usernameField.font = [UIFont fontWithName:fontName size:16.0f];
    
    self.passwordField.layer.cornerRadius = 3.0f;
    self.passwordField.placeholder = @"密码";
    self.passwordField.leftViewMode = UITextFieldViewModeAlways;
    UIView* leftView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.passwordField.leftView = leftView2;
    self.passwordField.font = [UIFont fontWithName:fontName size:16.0f];
    
    
    
    self.loginButton.backgroundColor = themeColor;
    self.loginButton.layer.cornerRadius = kCornerRadius;
    self.loginButton.titleLabel.font = [UIFont fontWithName:@"Optima-ExtraBlack" size:20.0f];
   
    
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.5f] forState:UIControlStateHighlighted];
    
    [_loginButton setPopingPressed];

}

- (IBAction)turnToNextField:(id)sender {
    [_passwordField becomeFirstResponder];
}

- (IBAction)finishEnterPassword:(id)sender {
    [sender resignFirstResponder];
    [self setUserInfo];
    [UIView down:self.view];
}


//获取输入框的字符串
- (void)setUserInfo{
    _myLogin.username = _usernameField.text;
    _myLogin.password = _passwordField.text;
    user = _usernameField.text;
    password = _passwordField.text;
}

//登陆
- (IBAction)Login:(id)sender {
    [self setUserInfo];
    //联网指示器
    if (!_activityIndicator) {
        _activityIndicator = [[UIActivityIndicatorView alloc]
                              initWithActivityIndicatorStyle:
                              UIActivityIndicatorViewStyleGray];
        CGSize captchaViewSize = _loginButton.bounds.size;
        _activityIndicator.hidesWhenStopped = YES;
        [_activityIndicator setCenter:CGPointMake(captchaViewSize.width/2, captchaViewSize.height/2)];
        [_loginButton addSubview:_activityIndicator];
    }
    [_activityIndicator startAnimating];
    
    __weak typeof(self) weakSelf = self;
    weakSelf.loginButton.enabled = NO;
    //发送post请求
    [[NetAPIManager sharedManager] request_Login_WithParams:[self.myLogin toParams] andBlock:^(id data, NSError *error) {
        weakSelf.loginButton.enabled = YES;
        [weakSelf.activityIndicator stopAnimating];
        
        if ([data isEqualToString:@"1"]) {
            [self showStatusBarSuccessStr:@"登录成功"];
            //[self dismissViewControllerAnimated:YES completion:NULL];

            [self performSegueWithIdentifier:@"loginsuccess" sender:self];
            //更新userid 和channelid
            [[NetAPIManager sharedManager]update_id:[self.myLogin toUpdateidParams] andBlock:^(id data, NSError *error) {
                
            }];
        }
        else if( [data isEqualToString:@"2"])
        {
            [self showHudTipStr:@"用户名或者密码错"];
            [weakSelf.usernameField PopingShake];
            [weakSelf.passwordField PopingShake];

        }
        else{
            [self showHudTipStr:@"用户已经登录"];

        }
    }];
    
  
}


/*!
 *  @brief  点击输入框的时候界面会上移
 *  @param sender uitextfield
 */
- (IBAction)touch:(id)sender {
    [UIView up:self.view length:210];
}

- (IBAction)touch2:(id)sender {
    [UIView up:self.view length:210];
}


@end
